<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/9/21 14:48
 * Modified Time: 2019/9/21 14:48
 * Version: 2.0
 */

namespace App\Modules\Service\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetServiceTypeAndServiceCriteria extends BaseCriterias
{
    protected $store_id;
    protected $barber_id;

    public function __construct($store_id, $barber_id)
    {
        $this->store_id = $store_id;
        $this->barber_id = $barber_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;
        $barber_id = $this->barber_id;
        return $model
            ->select([
                'dictionary_item.id',
                'fullname'
            ])
            ->with(['service' => function ($query) use ($store_id, $barber_id) {
                $query->select([
                    'store_service.id',
                    'name',
                    'type',
                    'price',
                    'store_service.royalty',
                    'cost'
                ])
                    ->when($barber_id, function ($query) use ($barber_id) {
                        $query
                            ->leftJoin('user_royalty', 'user_royalty.service_id', 'store_service.id')
                            ->where('status', 1)
                            ->where('user_id', $barber_id);
                    })
                    ->where('store_service.store_id', $store_id);
            }])
            ->where('parent_id', 4)
            ->orderBy('dictionary_item.id', 'ASC');
    }
}